package com.jsu.clubmanagementsystem.controller.application;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsu.clubmanagementsystem.common.BaseResponse;
import com.jsu.clubmanagementsystem.common.ErrorCode;
import com.jsu.clubmanagementsystem.common.PageQueryRequest;
import com.jsu.clubmanagementsystem.common.ResultUtils;
import com.jsu.clubmanagementsystem.constant.UserConstant;
import com.jsu.clubmanagementsystem.exception.BusinessException;
import com.jsu.clubmanagementsystem.model.dto.clubapplication.ClubApplicationAddRequest;
import com.jsu.clubmanagementsystem.model.dto.clubapplication.ClubApplicationQueryRequest;
import com.jsu.clubmanagementsystem.model.entity.ClubApplication;
import com.jsu.clubmanagementsystem.model.entity.User;
import com.jsu.clubmanagementsystem.model.vo.clubApplication.ClubApplicationListVO;
import com.jsu.clubmanagementsystem.model.vo.clubApplication.ClubApplicationVO;
import com.jsu.clubmanagementsystem.service.application.ClubApplicationService;
import com.jsu.clubmanagementsystem.service.user.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/club-application")
public class ClubApplicationController {

    @Resource
    private ClubApplicationService clubApplicationService;

    @Resource
    private UserService userService;

    @PostMapping("/add")
    public BaseResponse<Long> addClubApplication(@RequestBody ClubApplicationAddRequest addRequest, HttpServletRequest request) {
        if (addRequest == null || addRequest.getType() == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "参数为空");
        }
        Long userId = addRequest.getUserId();
        if (!userService.isExist(userId)) {
            throw new BusinessException(ErrorCode.NOT_FOUND, "用户不存在");
        }
        User loginUser = userService.getLoginUser(request);
        if (!loginUser.getId().equals(userId)) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "申请用户和登录用户不一致");
        }

        return ResultUtils.success(clubApplicationService.processClubApplication(addRequest));
    }

    /**
     * 查看社团申请列表(分页)
     *
     * @param queryRequest
     * @param request
     * @return
     */
    @PostMapping("/list")
    public BaseResponse<Page<ClubApplicationVO>> listClubApplicationVoByPage(@RequestBody ClubApplicationQueryRequest queryRequest, HttpServletRequest request) {
        if (queryRequest == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "参数为空");
        }
        User loginUser = userService.getLoginUser(request);
        if (loginUser.getUserRole().equals(UserConstant.USER_ROLE_USER)) {
            queryRequest.setUserId(loginUser.getId());
        } else if (loginUser.getUserRole().equals(UserConstant.USER_ROLE_MANAGER)) {
            queryRequest.setClubId(loginUser.getManagedClubId());
        }

        Page<ClubApplicationVO> page = clubApplicationService.listClubApplicationVOByPage(queryRequest);
        return ResultUtils.success(page);
    }

    /**
     * 查找活动申请
     *
     * @param id      申请ID
     * @param request HTTP请求
     * @return 申请详情
     */
    @GetMapping("/get")
    public BaseResponse<ClubApplicationVO> getApplicationVoById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAM_ERROR, "无效的申请ID");
        }

        ClubApplication clubApplication = clubApplicationService.getById(id);
        if (clubApplication == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND, "申请不存在");
        }
        ClubApplicationVO clubApplicationVO = clubApplicationService.getClubApplicationVO(clubApplication);

        User loginUser = userService.getLoginUser(request);
        if (loginUser.getUserRole().equals(UserConstant.USER_ROLE_MANAGER) &&
                !clubApplication.getClubId().equals(loginUser.getManagedClubId())) {
            throw new BusinessException(ErrorCode.NO_AUTH, "非本社团发出的申请");
        } else if (loginUser.getUserRole().equals(UserConstant.USER_ROLE_USER) &&
                !clubApplication.getUserId().equals(loginUser.getId())) {
            throw new BusinessException(ErrorCode.NO_AUTH, "非申请用户本人");
        }
        return ResultUtils.success(clubApplicationVO);
    }

    /**
     * 获取所有社团申请信息（分页)
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<ClubApplicationListVO>> getAllClubApplication(@RequestBody PageQueryRequest page){
        return ResultUtils.success(clubApplicationService.getAllList(page));
    }

    /**
     * 管理员删除申请
     */
    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteClubApplication(@RequestParam List<Long> ids){
        return ResultUtils.success(clubApplicationService.deleteClubApplication(ids));
    }

    /**
     * 新增社团审核申请
     */
    @PostMapping("/addApplication")
    public BaseResponse<Boolean> addClubApplications(@RequestBody ClubApplicationAddRequest add){
        return ResultUtils.success(clubApplicationService.addApplication(add));
    }
}
